package com.karma.client;


import java.util.ArrayList;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Button;
import com.karma.shared.Board;

public class Coloroid implements EntryPoint {

	private Label movesLabel = new Label("Moves : " + 0);
	private Label parLabel = new Label();
	private ArrayList<String> keyColor = new ArrayList<String>();
	private Board board;
	private Label[][] piecesLbl;
	private Grid gridColor;
	private Grid gridColorKeys;
	private AbsolutePanel absolutePanel;
	private MyClickHandler clickHandle;
	private Button[][] btns;
	private int startDim = 4;
	
	
	public void onModuleLoad() {
		RootPanel rootPanel = RootPanel.get();
		absolutePanel = new AbsolutePanel();
		rootPanel.add(absolutePanel, 0, 0);
		absolutePanel.setSize("500px", "500px");
		
		clickHandle = new MyClickHandler();
		
		btns = new Button[2][3];
				
		/**
		 * Used from the Colouroid.css file
		 */
		keyColor.add(0, "red-1");
		keyColor.add(1, "green-2");
		keyColor.add(2, "blue-3");
		keyColor.add(3, "cyan-4");
		keyColor.add(4, "magenta-5");
		keyColor.add(5, "yellow-6");
		
		/**
		 * TODO Customizable - number of colors used
		 */
		gridColorKeys = new Grid(2, 3);		// Currently using 6 colors but is customizable 
		absolutePanel.add(gridColorKeys, 0, 321);
		gridColorKeys.setSize("320px", "100px");
		
		for (int i = 0; i < 2 ; i++){
			for (int j = 0; j < 3 ; j++){
				btns[i][j] = new Button();
				btns[i][j].setSize("100px", "35px");
				String c = keyColor.get(i*3 + j);
				btns[i][j].setStyleName(c);
				btns[i][j].addClickHandler(clickHandle);
				gridColorKeys.setWidget(i, j, btns[i][j]);
			}
		}
		
		initPlayView(startDim);			//Initialize the puzzle 
		
		movesLabel.setSize("140px", "80px");
		absolutePanel.add(movesLabel, 10, 430);
		
		parLabel.setSize("140px", "80px");
		absolutePanel.add(parLabel, 100, 430);
		
		/**
		 * TODO Level / Grid piece changer functionality
		 * link with initPlayView()
		 */
		
		/**
		 * TODO Create View classes each for Welcome screen and Puzzle Board view
		 */
	}
	
	
	
	class MyClickHandler implements ClickHandler{

		@Override
		public void onClick(ClickEvent event) {
			Button btn = (Button)(event.getSource());
			
			int playStatus = board.updateMoves(piecesLbl[0][0].getStyleName(), btn.getStyleName());
			
			int currDim = board.getCurrDim();
			for (int i = 0; i < currDim ; i++){
				for (int j = 0; j < currDim ; j++){
					piecesLbl[i][j].setStyleName((String) board.getColorforPos(i, j));
				}
			}
			
			/**
			 * TODO Give silhouette or similar indication for currently changed colors
			 */
			
			DialogBox db = new DialogBox(true);
			
			switch (playStatus) {
			case Board.END_LEVEL:
				movesLabel.setText("Moves : " + 0);
				db.setText("Congratulations !! Next Level");
				db.show();
				initPlayView(++startDim);
				break;
			case Board.TRY_AGAIN:
				movesLabel.setText("Moves : " + 0);
				db.setText("Hard Luck !! Try Again ...");
				db.show();
				initPlayView(startDim);
				break;
			case Board.FALSE_MOVE:
//				do nothing
				break;

			default:
				movesLabel.setText("Moves : " + playStatus);
				break;
			}
			
		}
	}
	
	/**
	 * Initialize the game view with instance of Board
	 * @param gridDim
	 */
	public void initPlayView(int gridDim){
		piecesLbl = new Label[gridDim][gridDim];
		gridColor = new Grid(gridDim, gridDim);
		absolutePanel.remove(gridColor);
		absolutePanel.add(gridColor, 0, 0);
		gridColor.setSize("320px", "320px");
		gridColor.setCellSpacing(0);
		gridColor.setCellPadding(0);
		
		board = new Board(gridDim, keyColor.toArray());
		
		for (int i = 0; i < gridDim ; i++){
			for (int j = 0; j < gridDim ; j++){
				piecesLbl[i][j] = new Label();
				String piecesDimStr = 320 / gridDim + "px";
				piecesLbl[i][j].setSize(piecesDimStr, piecesDimStr);
				String c = (String) (board.getColorforPos(i, j));
				piecesLbl[i][j].setStyleName(c);
				gridColor.setWidget(i, j, piecesLbl[i][j]);
			}
		}
		
		parLabel.setText("Par : " + Board.par(gridDim));
	}
}
